﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace Akronus.TaskScheduler
{
    /// <summary>
    /// Wrapper on <see cref="System.Timers.Timer"/> class. 
    /// </summary>
    public class TaskTimer : ITaskTimer
    {
        private System.Timers.Timer timer = null;

        /// <summary>
        /// Gets or sets the interval at which to raise the Akronus.TaskScheduler.TaskTimer.Elapsed event.
        /// </summary>
        /// <value>The time, in milliseconds, between raisings of the Elapsed event. The default is 100 milliseconds.</value>
        public double Interval 
        {
            get { return timer.Interval; }
            set { timer.Interval  = value; }
        }

        /// <summary>
        /// Occurs when the interval elapses.
        /// </summary>
        public event ElapsedEventHandler Elapsed;

        /// <summary>
        /// Initializes a new instance of the <see cref="TaskTimer"/> class.
        /// </summary>
        public TaskTimer()
        {
            timer = new System.Timers.Timer();
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        }

        /// <summary>
        /// Starts raising the Akronus.TaskScheduler.TaskTimer.Elapsed event.
        /// </summary>
        public void Start()
        {
            timer.Enabled = true;
        }

        /// <summary>
        /// Stops raising the Akronus.TaskScheduler.TaskTimer.Elapsed event.
        /// </summary>
        public void Stop()
        {
            timer.Enabled = false;
        }

        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Elapsed(sender, e);
        }
    }
}
